/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class TakeSubstringGame {
public:
    int winningMove(int n);
};
inline string toS(int n) {
    char buff[10];
    sprintf(buff,"%d",n);
    return (string)buff;
}

int dp[1000001];
int go(int N) {
    int& ret = dp[N];
    if ( ret!=-1 )return ret;

    ret=0;
    string str = toS(N);
    int ln = LN(str);
    for (int i=0;i<ln;++i) {
        if ( str[i]=='0' )continue;
        int var = 0;
        for (int j=i;j<ln;++j) {
            var = (var*10) + (str[j]-'0');
            if ( ( i==0 && (j+1) == ln ) )continue;
            if ( var==0 )continue;
            if ( go( N-var )==0 ) {
                ret=1;
            }
        }
    }

    return ret;
}
inline int go2(int N) {
    int best = (int)1023456789;
    string str = toS(N);
    int ln = LN(str);
    for (int i=0;i<ln;++i) {
        if ( str[i]=='0' )continue;
        int var = 0;
        for (int j=i;j<ln;++j) {
            var = (10*var) + (str[j]-'0');
            if ( ( i==0 && (j+1) == ln ) )continue;
            if ( var==0 )continue;

            if ( go(N-var)==0 ) {
                best = min( best, var );
            }
        }
    }
    return best;
}

int TakeSubstringGame :: winningMove(int n) {
    int retval=0;
    SET(dp,-1);
    for (int i=0;i<=n;++i) {
        go(i);
    }
    if ( go(n)==0 ) {
        retval = -1;
    }
    else {
        retval = go2(n);;
    }
    return retval;
}



// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, int p0, bool hasAnswer, int p1) {
    cout << "Test " << testNum << ": [" << p0;
    cout << "]" << endl;
    TakeSubstringGame *obj;
    int answer;
    obj = new TakeSubstringGame();
    clock_t startTime = clock();
    answer = obj->winningMove(p0);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p1 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p1;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    int p0;
    int p1;

    {
        // ----- test 0 -----
        p0 = 5;
        p1 = -1;
        all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        p0 = 10;
        p1 = 1;
        all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        p0 = 17;
        p1 = -1;
        all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        p0 = 239;
        p1 = 9;
        all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        p0 = 566;
        p1 = 66;
        all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 5 -----
        p0 = 1000000;
        p1 = -1;
        all_right = KawigiEdit_RunTest(5, p0, true, p1) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
